Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Windows Shell API mapping: SHGetSpecialFolderPath #192

Merged
merged 3 commits into from
Feb 22, 2013

Conversation

headcrashing
Copy link
Contributor

Timothy and Daniel,

as discussed https://groups.google.com/forum/?fromgroups#!topic/jna-users/KUpYp8rzFwk before, here is the first of a few small extensions.

This one maps the Windows Shell API function "SHGetSpecialFolderPath" and provides a unit test for it. As we are porting some software 1:1 from C++ to Java, we needed to use this. I know that the same can be done with the already existing GetFolderPath, but we (and possible more) people like this (rather deprecated) function more, as it allows to be as simple as GetFolderPath(CSIDL), but has the option to create paths.

Would be great if you would accept this contribution.

More APIs are one the way. :-)

@headcrashing
Copy link
Contributor Author

As on Windows it is possible to map .ini files to be stored not on disk but in registry, using Java File API for accessing these is not sufficient. Hence we today added another commit providing GetPrivateProfileString, GetPrivateProfileInt and WritePrivateProfileString API mappings. Also, using these functions allow to get 100% the same syntax for parsing .ini files as native applications. We'd be glad if you would merge this into your code base. :-)

*/
public static final String getPrivateProfileString(final String appName, final String keyName, final String defaultValue, final String fileName) {
final char buffer[] = new char[1024];
Kernel32.INSTANCE.GetPrivateProfileString(appName, keyName, defaultValue, buffer, new DWORD(buffer.length), fileName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be checking error here and raising an exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, reading its description it looks like it cannot fail. Oh it comes from 16-bit times :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this function will never fail but simply return the default! If the buffer is too small, the result is simply truncated. If the file is not found, the default is applied. Both are not necessarily failures but most likely are accepted by people used to this function. Typically, it is wanted behaviour that the default is returned if the file, the appname, or the key is missing -- instead of getting an exception. I think nobody ever checked for GetLatestError after this function, as the default's job is to get in place in case of any problem. Users would be scared if there is an exception thrown instead of getting the default. So if you insist for sake of OOP, I can throw an exception. But my advise would be in this particular function not to throw, for the sake of "expected behaviour". :-)

BTW, yes, this comes from 16 bit times, but in fact is still in use in lots of 32 bit software. ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct. This is all good.

@dblock
Copy link
Member

dblock commented Feb 22, 2013

This is great. Can you please update CHANGES.md too?

dblock added a commit that referenced this pull request Feb 22, 2013
New Windows Shell API mapping: SHGetSpecialFolderPath
@dblock dblock merged commit 06fde95 into java-native-access:master Feb 22, 2013
mstyura pushed a commit to mstyura/jna that referenced this pull request Sep 9, 2024
… subtype (java-native-access#192)

Motivation:

We should override the flush / read method so we return the correct sub-type for method chaining.

Modifications:

Add missing overrides and implementations

Result:

Be able to correctly method chain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants